package kcms.message.service.impl;

import java.util.List;

import javax.annotation.Resource;

import kcms.db.Course;
import kcms.db.Message;
import kcms.db.Messagestatus;
import kcms.db.PageBean;
import kcms.db.PptAndContent;
import kcms.db.TeachStudent;
import kcms.db.ViewAssignScore;
import kcms.db.ViewMessage;
import kcms.message.dao.MessageDAO;
import kcms.message.service.MessageService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MessageServiceImpl implements MessageService{
	@Resource
	private MessageDAO messageDao;
	
	public MessageDAO getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDAO messageDao) {
		this.messageDao = messageDao;
	}

	public List<TeachStudent> getAllTeach(String tid) {
		// TODO Auto-generated method stub
		return this.messageDao.getAllTeach(tid);
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public boolean addMessage(Message m) {
		// TODO Auto-generated method stub
		return this.messageDao.addMessage(m);
	}

	public List<Message> getHisMessage(String tid) {
		// TODO Auto-generated method stub
		return this.messageDao.getHisMessage(tid);
	}

	public Course getOneCourse(String cid) {
		// TODO Auto-generated method stub
		return this.messageDao.getOneCourse(cid);
	}

	public List<Course> getAllCourse() {
		// TODO Auto-generated method stub
		return this.messageDao.getAllCourse();
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void deleteMessage(int messageid) {
		// TODO Auto-generated method stub
		this.messageDao.deleteMessage(messageid);
	}

	public List<Messagestatus> getHisMessagestatus(String sid) {
		// TODO Auto-generated method stub
		return this.messageDao.getHisMessagestatus(sid);
	}

	public Message getOneMessage(int messageid) {
		// TODO Auto-generated method stub
		return this.messageDao.getOneMessage(messageid);
	}

	public List<ViewMessage> getAllMessagesBySid() {
		// TODO Auto-generated method stub
		return this.messageDao.getAllMessagesBySid();
	}

	public List<ViewMessage> getAllMessagesByCondition(
			String condition) {
		// TODO Auto-generated method stub
		return this.messageDao.getAllMessagesByCondition(condition);
	}

	public PageBean getForPage(int pageSize, int page,String condition,String uid) {
		// TODO Auto-generated method stub
		int allRow = this.messageDao.getAllRowCount(condition,uid);  //总记录数
		int totalPage = PageBean.countTatalPage(pageSize, allRow); //总页数
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page); // 当前页
		List list = this.messageDao.getForPage(offset, length,condition,uid); //
		//把分页信息保存到Bean当中
		PageBean pageBean  = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void updateMessageStatus(Messagestatus messageStatus) {
		// TODO Auto-generated method stub
		this.messageDao.updateMessageStatus(messageStatus);
	}

	public Messagestatus getOneMessageStatusById(int messageid,String sid) {
		// TODO Auto-generated method stub
		return this.messageDao.getOneMessageStatusById(messageid,sid);
	}

	public ViewMessage getOneVMessage(int messageid,String sid) {
		// TODO Auto-generated method stub
		return this.messageDao.getOneVMessage(messageid,sid);
	}

	public List<ViewMessage> getAllMessagesBySender(String sender) {
		// TODO Auto-generated method stub
		return this.messageDao.getAllMessagesBySender(sender);
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)

	public void updateMessage(Message m) {
		// TODO Auto-generated method stub
		this.messageDao.updateMessage(m);
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void updateMessagestatus(Messagestatus ms) {
		// TODO Auto-generated method stub
		this.messageDao.updateMessagestatus(ms);
	}

	public List<Messagestatus> getMessagestatusById(int messageid) {
		// TODO Auto-generated method stub
		return this.messageDao.getMessagestatusById(messageid);
	}

	@Override
	public List<PptAndContent> listPptMessages(String sid) {
		// TODO Auto-generated method stub
		return this.messageDao.listPptMessages(sid);
	}

	@Override
	public List<ViewAssignScore> listAssignMessages(String condition, String sid) {
		// TODO Auto-generated method stub
		return this.messageDao.listAssignMessages(condition, sid);
	}
	
    public ViewAssignScore getOneAssignMessage(int assignmentid){
    	return this.messageDao.getOneAssignMessage(assignmentid);
    }

	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void updateAssignMessage(ViewAssignScore va) {
		// TODO Auto-generated method stub
		this.messageDao.updateAssignMessage(va);
	}

  
}
